<?php

namespace Home\Controller;

use Think\Controller;
use Think\Page;

class TypeController extends Controller
{
    function searchTypeList(){
        $type=D('type');
        $typecount = $type->count();
        $page=new Page($typecount,10);
        $typelist = $type->limit($page->firstRow.','.$page->listRows)->select();
        $pageshow = $page->show();
        $this->assign('typelist',$typelist);
        $this->assign('show',$pageshow);
        $this->display('type/typelist');
    }
    function  keywordsSearch(){
        $keywords=I('keywords');
        $type=D('type');
        $typecount = $type->count();
        $page=new Page($typecount,10);
        $pageshow = $page->show();
        $data_like['typename']=array('like',array("%$keywords%"));
        $ret=$type->where($data_like)->limit($page->firstRow.','.$page->listRows)->select();
        $this->assign('typelist',$ret);
        $this->assign('show',$pageshow);
        $this->display('typelist');
    }
    function addType(){
        $typemodle=new \Home\Model\TypeModle();
        $modelret=$typemodle->create();
        $typepic=uploadImg('typepic','./Public/typemoren/typemoren.png');
        $_POST['typepic'] = $typepic;
        if ($modelret){
            $addret=$typemodle->add($_POST);
            if ($addret){
                $this->redirect('type/searchTypeList');
            }
        }else{
            $this->error($typemodle->getError());
        }
    }
    function delType(){
        $typeid=I('typeid');
        $type=D('type');
        $delret=$type->where(array('typeid'=>$typeid))->delete();
        if ($delret){
            $this->redirect('type/searchTypeList');
        }else{
            $this->error('该物品已经被删除');
        }
    }
    function toUptype(){
        $typeid=I('typeid');
        $type=D('type');
        $typeone=$type->where(array('typeid'=>$typeid))->find();
        $this->assign('type',$typeone);
        $this->display('type/uptype');
    }
    function upType(){
        $typeid=I('typeid');
        $typemodle=new \Home\Model\TypeModle();
        $modelret=$typemodle->create();
        $retone=$typemodle->where(array('typeid'=>$typeid))->find();
        $typepic=uploadImg('typepic',$retone['typepic']);
        $_POST['typepic'] = $typepic;
        if ($modelret){
            $upret=$typemodle->where(array('typeid'=>$typeid))->save($_POST);
            if ($upret){
                $this->redirect('type/searchTypeList');
            }
        }else{
            $this->error($typemodle->getError());
        }
    }
    function isHave(){
        if (IS_AJAX){
            $typename=I('typename');
            $type=D('type');
            $ret =$type->where(array('typename'=>$typename))->select();
            if ($ret){
                $this->ajaxReturn(
                    array(
                        'info'=>'1',
                    )
                );
            }
        }
    }
}
